Microcontrollers
At its simplest, a microcontroller is essentially a small computer. It is often used to simplify design of electrical systems. It is especially useful in creating control systems, such as those found on ROVs. This page will explain the basics of using microcontrollers. This page will cover working with the PIC18F4685, but other models will be similar. Its datasheet, containing important information about its operation can be found at the following link: http://ww1.microchip.com/downloads/en/DeviceDoc/39761c.pdf Input and Output While the pins on microcontrollers can have many uses (see the figure to the left), one of the most basic and important is input/output (I/O). While these functionalities are grouped together, each pin can only be used for one at a time. For the purposes of this page, I will be focusing on output. As the name suggests, output pins are used to affect something outside of the microcontroller. For example, by connecting an LED (a light) to pin PC3 and setting it as an output and high, the LED can be switched on. Microcontroller I/O are digital, meaning they have two possible values: high and low. I/O pins are divided in two ways. The first is into ports lettered A through E. Within the ports, pins are further divided by numbers ranging from 0-7. Going back to the LED example, pin PC3 is the third pin on port C. To see which pin identifier corresponds to which physical pin on the device, refer to the pinout on the datasheet. Registers Registers are basically special variables that control operation of the microcontroller. They are typically 8 bits, meaning they have 8 binary digits to modify. Some registers, however, are larger. The beginning bit of an 8 bit register is bit 0, while the final bit is bit 7. Using Registers to Control I/O Registers have many functions and uses, but one of the simplest and most useful is operation of the I/O pins. As mentioned earlier, I/O pins can either be selected as inputs or outputs. If they are outputs, they can be set to high or low. Both things are controlled by registers. Going back to the LED example, the TRISC register controls whether each pin on port C is an input or output, while the individual bits correspond to each pin within port C, with a 0 being an output and a 1 being an input. For example, if we want pin PC3 to be an output and the rest of port C's pins to be inputs, we need to set TRISC = 0b11110111; In general, TRISN corresponds to port N. To actually set the pin high, we need to modify the LATC register in much the same way, except this time we want to set the third bit to 1 instead. We'll assume that the rest of LATC are 0's: LATC = 0b00001000; Manipulating Individual Bits in Registers Often, we may want to modify individual bits of registers without affecting the rest of the register. In out last example, we would have liked to change only the third bit of TRISC and LATC instead of the whole register. This can be done by adding bits to the end of the register name and a ".". After this, the various names of the bits should pop up. For example, to set the third bit of TRISC to 0: TRISCbits.TRISC3 = 0; This is similar for LATC: LATCbits.LATC3 = 1; This sets pin PC3 to an output and high without changing what the rest of the pins are doing. Category:Microcontroller Functions Category:Control